package br.com.ss.centralaamar.component;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

public class SendMailTLS {

	public static void main(String[] args) throws IOException {

		final String username = "claudemirramosferreira@gmail.com";
		final String password = "vedita1234";

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(username));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(username));
			message.setSubject("Testando o envio de email ");
			message.setText("pequeno eeee," + "\n\n wwwwwww 0000000");

			// mensagem que vai no corpo do email
			MimeBodyPart mbpMensagem = new MimeBodyPart();
			mbpMensagem.setText(".Teste de Enviar");

			// partes do email
			Multipart mp = new MimeMultipart();
			mp.addBodyPart(mbpMensagem);

			String imagem = "C:\\focusHath2011.jpg";

			InputStream inputStream = new FileInputStream(imagem);

			// setando o anexo
			MimeBodyPart mbpAnexo = new MimeBodyPart();
			mbpAnexo.setDataHandler(new DataHandler(new ByteArrayDataSource(
					inputStream, "image/png")));
			mbpAnexo.setFileName(imagem);
			mp.addBodyPart(mbpAnexo);

			// assunto do email
			message.setSubject("Envio de mensagem - Igreja Adventista");

			// seleciona o conteudo
			message.setContent(mp);

			Transport.send(message);

			System.out.println("Doneeeee");
			
//			HtmlEmail html = new HtmlEmail();
//			html.setMailSession(session);
			

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}